package com.hnkywl.sts.service.taskdeal;

import com.hnkywl.sts.action.jhgl.SendPlanImport;
import com.hnkywl.sts.entity.jhgl.Bgjhhzmx;
import com.hnkywl.sts.service.jhgl.BgjhhzService;
import com.hnkywl.sts.service.jhgl.BgjhhzmxService;
import com.hnkywl.sts.service.sys.WzbmService;
import com.hnkywl.sts.webservice.bg.request.SendPlanImportResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-5-12
 * Time: 下午5:00
 * To change this template use File | Settings | File Templates.
 * 计划导入到保供系统
 */
@Service
@Transactional
public class DoPlanImport {
    // 定义日志
    private static Log log = LogFactory.getLog(DoPlanImport.class);

    // 保供计划汇总明细service
    private BgjhhzmxService bgjhhzmxService;

    // 保供计划汇总service
    private BgjhhzService bgjhhzService;

    private SendPlanImport sendPlanImport;

    public void importPlan() {
        List<Bgjhhzmx> bgjhhzmxList = this.bgjhhzmxService.findFailList();
        try {
            log.info("计划导入开始执行importPlan......................");
            if (bgjhhzmxList != null && bgjhhzmxList.size() > 0) {
                // 把数据发送给保供接口
                bgjhhzService.updateKsdr(bgjhhzmxList);
                List<SendPlanImportResult> resultList = sendPlanImport.sendDataBg(bgjhhzmxList,"定时服务导入");
                if(!CollectionUtils.isEmpty(resultList)){
                    bgjhhzService.updateSfcg(bgjhhzmxList,resultList);
                } else {
                    bgjhhzService.dealWhenExcept(bgjhhzmxList,"调用接口异常，到日志查看异常信息！");
                }
            }
            log.info("计划导入成功importPlan......................");
        } catch (Exception e) {
            //产生异常，所有数据置为"业务科已处理状态"
            bgjhhzService.dealWhenExcept(bgjhhzmxList,e.getMessage());
            log.info("计划导入开始执行错误：" + e);
        }
    }

    @Autowired
    public void setBgjhhzmxService(BgjhhzmxService bgjhhzmxService) {
        this.bgjhhzmxService = bgjhhzmxService;
    }

    @Autowired
    public void setBgjhhzService(BgjhhzService bgjhhzService) {
        this.bgjhhzService = bgjhhzService;
    }

    @Autowired
    public void setSendPlanImport(SendPlanImport sendPlanImport) {
        this.sendPlanImport = sendPlanImport;
    }
}
